package com.youhome.service.service.impl;


import com.github.pagehelper.PageHelper;
import com.youhome.common.config.OSSConfig;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultCode;
import com.youhome.service.mapper.ListHubPhotoMapper;
import com.youhome.service.model.CustomCompanyTokenDetails;
import com.youhome.service.model.ListHub;
import com.youhome.service.model.ListHubPhoto;
import com.youhome.service.service.BUserService;
import com.youhome.service.service.ListHubPhotoService;
import com.youhome.service.service.ListHubService;
import com.youhome.service.service.OSSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chen365 on 2018/05/09.
 */
@Service
public class ListHubPhotoServiceImpl implements ListHubPhotoService {
    @Autowired
    private OSSService ossService;
    @Autowired
    private ListHubPhotoService listHubPhotoService;
    @Autowired
    private BUserService bUserService;
    @Autowired
    private ListHubPhotoMapper listHubPhotoMapper;
    @Autowired
    private OSSConfig ossConfig;
    @Autowired
    private ListHubService listHubService;

    @Override
    public List<ListHubPhoto> selectByBean(ListHubPhoto record) {
        return listHubPhotoMapper.selectByBean(record);
    }

    @Override
    public int insertBatch(List<ListHubPhoto> list) {
        return listHubPhotoMapper.insertBatch(list);
    }

    @Override
    public int deletePhotoByBean(ListHubPhoto record) {
        return listHubPhotoMapper.deleteByBean(record);
    }

    @Override
    public List<String> selectTop10ByBean(String houseId) {
        return listHubPhotoMapper.selectTop10ByBean(houseId);
    }

    @Override
    public List<String> selectPhotoByHouseId(String houseId) {
        ListHubPhoto record = new ListHubPhoto();
        record.setHouseId(houseId);
        record.setIsDownload(true);
        record.setDownloadStatus(1);
        record.setRepeat(false);
        List<ListHubPhoto> list = listHubPhotoMapper.selectByBean(record);
        List<String> photos = new ArrayList<>();
        for (ListHubPhoto listHubPhoto : list) {
            photos.add(listHubPhoto.getOssUrl());
        }
        return photos;
    }

    @Override
    public void deletePhoto(String houseId, String ossUrl) throws CustomException {
        if (StringUtils.isEmpty(houseId) || StringUtils.isEmpty(ossUrl)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        ListHubPhoto listHub = new ListHubPhoto();
        listHub.setHouseId(houseId);
        listHub.setOssUrl(ossUrl);
        listHub.setSource("YouHome");

        Long companyId = bUserService.getCompanyId();
        if (StringUtils.isEmpty(companyId)) {
            throw new CustomException(ResultCode.EXPIRED_LOGIN);
        }

        if (listHubPhotoMapper.deleteByBean(listHub) > 0) {
            if (!ossService.delete(ossUrl)) {
                throw new CustomException(ResultCode.OSS_PHOTO_DELETE_FAIL);
            }
        }
    }

    @Override
    public void delete(String houseId, String photoUrl, CustomCompanyTokenDetails customCompanyTokenDetails) throws CustomException {
        if (StringUtils.isEmpty(houseId) || StringUtils.isEmpty(photoUrl)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        ListHub listHub = listHubService.selectByPrimaryKey(houseId);

        if (listHub == null) {
            throw new CustomException(ResultCode.HOUSE_INEXISTENCE);
        }

        ListHubPhoto record = new ListHubPhoto();
        record.setHouseId(houseId);
        record.setOssUrl(photoUrl);
        record.setSource("housing"); // 第三方来源

        if (listHubPhotoMapper.selectExistByHouseIdAndOSSUrl(record) == 0) {
            throw new CustomException(ResultCode.NOT_EXIST_HOUSE_PHOTO);
        }

        //禁止删除非本公司房源
        if (!listHub.getCompanyId().equals(customCompanyTokenDetails.getCompanyId())) {
            throw new CustomException(ResultCode.ILLEGAL_OPERATION);
        }

        if (listHubPhotoMapper.deleteByBean(record) > 0) {
            if (!ossService.delete(photoUrl)) {
                throw new CustomException(ResultCode.OSS_PHOTO_DELETE_FAIL);
            }
        }
    }

    @Override
    public List<String> selectPhotoOfMobile(String houseId) throws CustomException {

        if (StringUtils.isEmpty(houseId)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }

        ListHubPhoto record = new ListHubPhoto();
        record.setHouseId(houseId);
        record.setIsDownload(true);
        record.setDownloadStatus(1);
        record.setRepeat(false);

        List<ListHubPhoto> list = listHubPhotoService.selectByBean(record);
        List<String> photos = new ArrayList<>();
        for (ListHubPhoto listHubPhoto : list) {
            String ossUrl = listHubPhoto.getOssUrl();
            if (!StringUtils.isEmpty(ossUrl)) {
                ossUrl = ossConfig.getDomain() + ossUrl;
                photos.add(ossUrl);
            }
        }
        return photos;
    }

    @Override
    public List<String> selectOSSUrlByBean(String houseId, Integer count) {
        if (StringUtils.isEmpty(count) || count < 1) {
            count = 6;
        }
        ListHubPhoto record = new ListHubPhoto();
        record.setHouseId(houseId);
        record.setIsDownload(true);
        record.setDownloadStatus(1);
        record.setRepeat(false);
        PageHelper.startPage(1, count);
        List<String> list = listHubPhotoMapper.selectOSSUrlByBean(record);

        // 大于1条时，截取偶数条
        if (list.size() > 1 && list.size() % 2 != 0) {
            return list.subList(0, list.size() - 1);
        }
        return list;
    }
}
